package com.breakPointUpload.test;

import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by pengfei on 16/7/13.
 */
public class Client {
    public static void main(String[] args) throws Exception {
        URL url = new URL("http://www.sjtu.edu.cn/down.zip");
        HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();

        //设置User-Agent
        httpConnection.setRequestProperty("User-Agent", "NetFox");
        //设置断点续传的开始位置
        httpConnection.setRequestProperty("RANGE", "bytes=2000070");
        //获得输入流
        InputStream input = httpConnection.getInputStream();

        RandomAccessFile oSavedFile = new RandomAccessFile("down.zip","rw");
        long nPos = 2000070;
        // 定位文件指针到 nPos 位置
        oSavedFile.seek(nPos);
        byte[] b = new byte[1024];
        int nRead;
        // 从输入流中读入字节流，然后写到文件中
        while((nRead=input.read(b,0,1024)) > 0){
            oSavedFile.write(b,0,nRead);
        }
    }
}
